package edu.sandau.basic.service.impl;

import edu.sandau.basic.domain.User;
import edu.sandau.basic.domain.UserExample;
import edu.sandau.basic.global.GlobalEnum;
import edu.sandau.basic.mapper.UserExtMapper;
import edu.sandau.basic.service.UserService;
import edu.sandau.basic.utils.Converter;
import edu.sandau.basic.vo.UserVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;



@Service
public class UserServiceImpl implements UserService {
    @Resource
    //private UserMapper userMapper;
    private UserExtMapper userExtMapper;
    @Override
    public GlobalEnum.ErrorCode addUser(UserVo userVo){
        GlobalEnum.ErrorCode errorCode = GlobalEnum.ErrorCode.USER_ADD_FAILED;
        User user = new User();
        user.setUsername(userVo.getUserName());
        user.setPassword(userVo.getPassword());
        user.setBirthday(Converter.localDateToDate(userVo.getBirthday()));
        user.setMobile(userVo.getMobile());
        user.setEnName(userVo.getEnName());
        user.setEmail(userVo.getEmail());
        user.setIsActive(true);
        user.setProfileUrl(userVo.getProfileUrl());
        user.setCreateperson(1);
        user.setUpdateperson(1);
        user.setCreatetime(Converter.localDateTimeToDate(LocalDateTime.now()));
        user.setUpdatetime(Converter.localDateTimeToDate(LocalDateTime.now()));
        int result = userExtMapper.insertSelective(user); //只写非空列
        if(result == 1){
            errorCode = GlobalEnum.ErrorCode.OK;
        }
        return errorCode;
    }

    @Override
    public GlobalEnum.ErrorCode userLogin(String username, String password) {
        //userMapper.
        //去数据库里面根据用户名找出密码，判断密码是否一致？
        User user = userExtMapper.selectByUserName(username);
        //User user = null;
        if(user != null && user.getPassword().equals(password)){
            return GlobalEnum.ErrorCode.OK;
        }
        return GlobalEnum.ErrorCode.USER_NOT_EXIST;
    }


    @Override
    public List<User> listUsers(String username, Boolean isActive) {
        UserExample ex = new UserExample();
        UserExample.Criteria c = ex.createCriteria();
        if (username != null && !username.isEmpty()) {
            c.andUsernameLike("%" + username + "%");
        }
        if (isActive != null) {
            c.andIsActiveEqualTo(isActive);
        }
        ex.setOrderByClause("id desc");
        return userExtMapper.selectByExample(ex);
    }

    @Override
    public User getById(Integer id) {
        return userExtMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional
    public GlobalEnum.ErrorCode updateUser(Integer id, UserVo vo) {
        User db = userExtMapper.selectByPrimaryKey(id);
        if (db == null) return GlobalEnum.ErrorCode.USER_NOT_EXIST;

        // 可按需选择允许修改的字段
        if (vo.getUserName() != null) db.setUsername(vo.getUserName());
        if (vo.getPassword() != null) db.setPassword(vo.getPassword());
        if (vo.getEmail() != null) db.setEmail(vo.getEmail());
        if (vo.getMobile() != null) db.setMobile(vo.getMobile());
        if (vo.getEnName() != null) db.setEnName(vo.getEnName());
        if (vo.getBirthday() != null) db.setBirthday(Converter.localDateToDate(vo.getBirthday()));
        if (vo.getProfileUrl() != null) db.setProfileUrl(vo.getProfileUrl());
        db.setUpdatetime(Converter.localDateTimeToDate(LocalDateTime.now()));
        db.setUpdateperson(1);
        if (vo.getIsActive() != null) db.setIsActive(vo.getIsActive());

        int rows = userExtMapper.updateByPrimaryKeySelective(db);
        return rows == 1 ? GlobalEnum.ErrorCode.OK : GlobalEnum.ErrorCode.USER_MODIFY_FAILED;
    }

    @Override
    @Transactional
    public GlobalEnum.ErrorCode deleteUser(Integer id) {
        int rows = userExtMapper.deleteByPrimaryKey(id);
        return rows == 1 ? GlobalEnum.ErrorCode.OK : GlobalEnum.ErrorCode.USER_DELETE_FAILED;
    }
}
